package hsl.study.controller;


import hsl.study.utils.DateTimeTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/8/4 14:09
 * @Version: 1.0
 */


@RestController
@RequestMapping("/toolCalling")
public class ToolCallingController {

    @Resource
    private ChatModel chatModel;
    @Resource
    private ChatClient chatClient;

    /**
     * http://localhost:7013/toolCalling/chat1?question=介绍一下java
     * @param question
     * @return
     */
    @GetMapping("/chat1")
    public String chat1(@RequestParam(name = "question", defaultValue = "你是谁，现在几点了") String question) {
        // 1.将工具注册到工具表
        ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools());

        // 2. 将工具集设置到Chat聊天中
        ToolCallingChatOptions options = ToolCallingChatOptions.builder()
                .toolCallbacks(tools)
                .build();
        // 3. 构建提示词
        Prompt prompt = new Prompt(question, options);
        return chatModel.call(prompt).getResult().getOutput().getText();
    }

    /**
     * http://localhost:7013/toolCalling/chat2?question=介绍一下java
     * @param question
     * @return
     */
    @GetMapping("/chat2")
    public Flux<String> chat2(@RequestParam(name = "question", defaultValue = "你是谁，现在几点了") String question) {
        return chatClient
                .prompt(question)
                .tools(new DateTimeTools())
                .stream()
                .content();
    }
}
